package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //套餐管理
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //起售停售功能
    @ApiOperation("启售停售功能")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result setStatus( @PathVariable Long status ,Long id){
        log.info("[启售停售功能]status:{} id:{} ",status, id);
        setmealService.setStatus(status,id);
        return Result.success();
    }


    //修改套餐先回显
    @GetMapping("/{id}")
    @ApiOperation("套餐回显")
    public Result<SetmealVO> getSetmeal(@PathVariable Long id){
       // log.info("[套餐回显]id:{}",id);
       SetmealVO setmealVO = setmealService.selectSetmeal(id);

        return Result.success(setmealVO);
    }
    @ApiOperation("修改套餐信息")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public   Result setSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("[修改套餐]setmealDTO:{}",setmealDTO);
        //根据接受信息 修改数据库内容
        setmealService.setSetmeal(setmealDTO);
        return Result.success();

    }


    @ApiOperation("批量删除套餐信息")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids){
        log.info("[获取id信息]ids：{}",ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();

    }
    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
    public Result<PageResult> pageSelectSetmeal(SetmealPageQueryDTO pageQueryDTO){
      //  log.info("[分页查询套餐]pageQueryDTO：{}",pageQueryDTO);
        PageResult pageResult = setmealService.pageSelectSetmeal(pageQueryDTO);
        return Result.success(pageResult);
    }


    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result<PageResult> addSetmeal(@RequestBody SetmealDTO setmealDTO) {
       // log.info("[新增套餐]setmealDTO:{}",setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }
}


